package com.lottery.api.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.api.annotation.CoerceLoginAop;
import com.lottery.api.service.MemberInfoService;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.ActivityInfo;
import com.lottery.common.entity.FeedbackInfo;
import com.lottery.common.entity.MemberInfo;
import com.lottery.common.entity.MessageInfo;
import com.lottery.common.service.DecryptToObjectService;
import com.lottery.common.service.FeedbackInfoService;
import com.lottery.common.service.MessageInfoService;
import com.lottery.common.service.SysConfigService;
import com.lottery.common.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.lottery.common.contants.Constants.*;

@RestController
@RequestMapping("/message")
@Slf4j
@Api(value = "/message",tags="信息操作类")
public class MessageController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private ReQueryResult result = new ReQueryResult();

    @Autowired
    private MemberInfoService memberInfoService;
    @Resource
    private DecryptToObjectService decryptToObjectService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private FeedbackInfoService feedbackInfoService;
    @Autowired
    private MessageInfoService messageInfoService;
    @Autowired
    private StringRedisTemplate redisTemplate;


    @ApiOperation(value = "获得消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index",value = "页码 ",dataType="Integer"),
            @ApiImplicitParam(name = "length",value = "长度",dataType="Integer"),
            @ApiImplicitParam(name = "type",value = "消息类型 1 系统消息",dataType="Integer"),
            @ApiImplicitParam(name = "isRead",value = "是否已读 0 已读 1未读",dataType="Integer")

    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    @CoerceLoginAop
    public ReQueryResult<MessageInfo> message(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
                                              Integer index,Integer length,
                                              Integer type,Integer isRead){
        result = new ReQueryResult();
        MessageInfo param = new MessageInfo();
        param.setMemberId(memberInfo.getId());
        param.setIsRead(isRead);
        param.setType(type);
        QueryWrapper<MessageInfo> queryWrapper = new QueryWrapper<>(param);
        queryWrapper.eq("id_del",TYPE_ONE);
        queryWrapper.orderByDesc("id");

        Page<MessageInfo> page = new Page<>(index,length);
        IPage<MessageInfo> infos=messageInfoService.page(page,queryWrapper);
        for(MessageInfo info:infos.getRecords()){
            info.setMsgTime(DateUtil.formatToDay(info.getCreateTime()));
        }

        result.setData(infos.getRecords());
        result.success(SUCCESS_DESC);
        return result;
    }

    @ApiOperation(value = "获得消息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "消息类型 1 系统消息",dataType="Integer"),
            @ApiImplicitParam(name = "isRead",value = "是否已读 0 已读 1未读",dataType="Integer")
    })
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @CoerceLoginAop
    public ReQueryResult count(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
                                              Integer type,Integer isRead){
        result = new ReQueryResult();
        MessageInfo param = new MessageInfo();
        param.setMemberId(memberInfo.getId());
        param.setIsRead(isRead);
        param.setType(type);
        QueryWrapper<MessageInfo> queryWrapper = new QueryWrapper<>(param);
        Integer count=messageInfoService.count(queryWrapper);
        result.setData(count);
        result.success(SUCCESS_DESC);
        return result;
    }


    @ApiOperation(value = "设置单个消息已读、删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",dataType="Integer"),
            @ApiImplicitParam(name = "isRead",value = "0 已读",dataType="Integer"),
            @ApiImplicitParam(name = "isDel",value = "0 删除",dataType="Integer")
    })
    @RequestMapping(value = "", method = RequestMethod.POST)
    @CoerceLoginAop
    public ReQueryResult setMsgRead(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
                                     Long id,Integer isRead,Integer isDel){
        result=new ReQueryResult();
        MessageInfo info=new MessageInfo();
        info.setId(id);
        info.setIsRead(isRead);
        info.setIsDel(isDel);
        Boolean flag=messageInfoService.updateById(info);
        result.success(SUCCESS_DESC);
        if (!flag)
            result.error(UPDATE_FAILED_DESC);
        return  result;
    }

    @ApiOperation(value = "设置全部消息已读")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "id",dataType="Integer"),
//            @ApiImplicitParam(name = "isRead",value = "是否已读 0 已读 1未读",dataType="Integer")
    })
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @CoerceLoginAop
    public ReQueryResult setAllMsgRead(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
                                    @PathVariable Long id){
        result=new ReQueryResult();
        MessageInfo info=new MessageInfo();
        info.setMemberId(memberInfo.getId());
        UpdateWrapper<MessageInfo> updateWrapper = new UpdateWrapper<>(info);
        updateWrapper.set("is_read",TYPE_ZERO);
        Boolean flag=messageInfoService.update(info,updateWrapper);
        result.success(SUCCESS_DESC);
        if (!flag)
            result.error(UPDATE_FAILED_DESC);
        return  result;
    }

    @ApiOperation(value = "意见反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",dataType="Integer"),
            @ApiImplicitParam(name = "isRead",value = "0 已读",dataType="Integer"),
            @ApiImplicitParam(name = "isDel",value = "0 删除",dataType="Integer")
    })
    @RequestMapping(value = "/feedback", method = RequestMethod.POST)
    @CoerceLoginAop
    public ReQueryResult feedback(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
                                  FeedbackInfo info){
        result=new ReQueryResult();

        info.setMemberId(memberInfo.getId());
        Boolean flag=feedbackInfoService.save(info);
        result.success(SUCCESS_DESC);
        if (!flag)
            result.error(UPDATE_FAILED_DESC);
        return  result;
    }

}
